package com.l2jserver.gameserver.instancemanager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.l2jserver.Config;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import com.l2jserver.gameserver.network.L2GameClient;
import com.l2jserver.gameserver.util.Util;

/**
 * @author L0ngh0rn
 */
public class IpManager
{
	private static final Logger _log = Logger.getLogger(IpManager.class.getName());
	
	private static class SingletonHolder
	{
		protected static final IpManager _instance = new IpManager();
	}
	
	public static final IpManager getInstance()
	{
		return SingletonHolder._instance;
	}
	
	public IpManager()
	{
		_log.log(Level.INFO, "Verificando Ips .....");
	}
	
	private boolean testMultiBox(L2PcInstance activeChar, Integer numberBox, L2PcInstance[] world)
	{
		Map<String, List<L2PcInstance>> ipMap = new HashMap<>();
		for (L2PcInstance player : world)
		{
			if (player.getAdress() != null)
			{
				if ((player.getClient() == null) || player.getClient().isDetached())
				{
					continue;
				}
				
				if (!compareAdress(activeChar.getAdress(), player.getAdress()))
				{
					String ip = joinIps(player.getAdress());
					if (ipMap.get(ip) == null)
					{
						ipMap.put(ip, new ArrayList<L2PcInstance>());
					}
					ipMap.get(ip).add(player);
					if (ipMap.get(ip).size() >= numberBox)
					{
						return true;
					}
				}
			}
		}
		return false;
	}
	
	/**
	 * @param activeChar
	 * @param numberBox
	 * @param world
	 * @param forcedLogOut
	 * @return
	 */
	public boolean validBox(L2PcInstance activeChar, Integer numberBox, L2PcInstance[] world, Boolean forcedLogOut)
	{
		L2GameClient client = activeChar.getClient();
		
		if (testMultiBox(activeChar, numberBox, world))
		{
			if (forcedLogOut)
			{
				_log.warning("Multibox Protection: " + client.getConnection().getInetAddress().getHostAddress() + " was trying to use over " + numberBox + " clients!");
				Util.handleIllegalPlayerAction(activeChar, "Multibox Protection: " + client.getConnection().getInetAddress().getHostAddress() + " was trying to use over " + numberBox + " clients!", Config.MULTIBOX_PROTECTION_PUNISH);
			}
			return true;
		}
		// _log.log(Level.INFO, "Multibox Protection:" + client.getConnection().getInetAddress().getHostAddress() + " has pased");
		return false;
	}
	
	private boolean compareAdress(String[] a1, String[] a2)
	{
		if (a1.length != a2.length)
		{
			return false;
		}
		
		int equal = 0;
		for (int i = 0; i < a1.length; i++)
		{
			if (a1[i].equalsIgnoreCase(a2[i]))
			{
				equal++;
			}
		}
		if (equal == a1.length)
		{
			return false;
		}
		return true;
	}
	
	private String joinIps(String[] ips)
	{
		StringBuilder ip = new StringBuilder();
		for (String t : ips)
		{
			ip.append(t);
			ip.append("|");
		}
		return ip.toString();
	}
}
